package com.igoal.dto;

import java.util.Date;

import com.igoal.entity.ActionTypeEnum;
import com.igoal.entity.ComponentEnum;
import com.igoal.entity.User;
import com.igoal.entity.UserActivity;
import com.igoal.entity.UserInfo;
import com.igoal.service.paging.PageItem;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.StringUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;
import com.igoal.web.model.DummySettings;

public class UserActivityDTO extends BaseDTO<UserActivity> implements
		PageItem<Long> {
	private String actionOwnerName;
	private String actionOwnerDisplayName;
	private String actionOwnerAvatar;
	private String action;
	private String component;
	private Long componentId;
	private boolean read;

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getComponent() {
		return component;
	}

	public void setComponent(String component) {
		this.component = component;
	}

	public Long getComponentId() {
		return componentId;
	}

	public void setComponentId(Long componentId) {
		this.componentId = componentId;
	}

	public void setActionOwnerName(String actionOwnerName) {
		this.actionOwnerName = actionOwnerName;
	}

	public String getActionOwnerName() {
		return actionOwnerName;
	}

	public boolean getRead() {
		return read;
	}

	public void setRead(boolean read) {
		this.read = read;
	}

	public String getActionOwnerAvatar() {
		return actionOwnerAvatar;
	}

	public void setActionOwnerAvatar(String actionOwnerAvatar) {
		this.actionOwnerAvatar = actionOwnerAvatar;
	}

	public String getOwnerDisplayName() {
		return actionOwnerDisplayName;
	}

	public void setOwnerDisplayName(String ownerDisplayName) {
		this.actionOwnerDisplayName = ownerDisplayName;
	}

	public Long getSortedValue() {
		return updateDate;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "updateDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updatedSince = CalendarUtils
					.calculateMinutesDifferentBeforeNow(date);
			this.updateDate = date.getTime();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "actionOwner")
	public void initActionOwner(Object entity) {
		if (entity != null && entity instanceof User) {
			User user = (User) entity;
			actionOwnerName = user.getUsername();
			actionOwnerAvatar = user.getAvatar();
			if (StringUtils.isBlank(actionOwnerAvatar)) {
				actionOwnerAvatar = DummySettings.DEFAULT_AVATAR_URL;
			}

			UserInfo info = user.getUserInfo();
			String firstName = info.getFirstName();
			String midleName = info.getMiddleName();
			String lastName = info.getLastName();

			actionOwnerDisplayName = "";
			if (StringUtils.isNotBlank(lastName)) {
				actionOwnerDisplayName += lastName + " ";
			}
			if (StringUtils.isNotBlank(midleName)) {
				actionOwnerDisplayName += midleName + " ";
			}
			if (StringUtils.isNotBlank(firstName)) {
				actionOwnerDisplayName += firstName;
			}
			actionOwnerDisplayName = StringUtils.stripToNull(actionOwnerDisplayName);
			if (actionOwnerDisplayName == null) {
				actionOwnerDisplayName = actionOwnerName;
			}
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "actionType")
	public void initActionType(Object entity) {
		if (entity != null && entity instanceof ActionTypeEnum) {
			ActionTypeEnum actionType = ActionTypeEnum.valueOf(entity
					.toString());
			action = actionType.toString();
		}
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "component")
	public void initComponentType(Object entity) {
		if (entity != null && entity instanceof ComponentEnum) {
			ComponentEnum componentType = ComponentEnum.valueOf(entity
					.toString());
			component = componentType.toString();
		}
	}
}
